package com.tbit.uqbike.service.business.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.CodeBoundMachine;
import com.tbit.uqbike.object.pojo.ColorCode;
import com.tbit.uqbike.object.pojo.Machine;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.Page;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.base.MachineService;
import com.tbit.uqbike.service.business.ColorCodeService;
import com.tbit.uqbike.service.rpcservice.PurviewService;
import com.tbit.uqbike.webmanager.dao.core.CodeBoundMachineDao;
import com.tbit.uqbike.webmanager.dao.core.ColorCodeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:58 2023/8/31
 */
@Service
public class ColorCodeServiceImpl extends ServiceImpl<ColorCodeDao, ColorCode> implements
        ColorCodeService {

    @Autowired
    private CodeBoundMachineDao codeBoundMachineDao;
    @Autowired
    private MachineService machineService;
    @Autowired
    private PurviewService purviewService;

    @Override
    public PageResult<CodeBoundMachine> queryPage(Integer accountId, String machineNO, String content, Integer codeId,
                                                  Page page) {
        if (null != page) {
            PageHelper.startPage(page.getPageNO(), page.getRowCount());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("codeId", codeId);
        map.put("machineNO", machineNO);
        map.put("content", content);
        List<CodeBoundMachine> list = codeBoundMachineDao.queryPage(map);
        PageInfo<CodeBoundMachine> pageInfo = new PageInfo<>(list);
        return new PageResult<>(page.getPageNO(), page.getRowCount(), pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getList());
    }

    @Override
    public BatchOperationMsgVO<String> add(List<String> machineNOs, Integer codeId, LoginInfo loginInfo) {
        BatchOperationMsgVO<String> batchOperationMsgVO = new BatchOperationMsgVO<>();
        List<Machine> machines = machineService.getByMachineNOs(machineNOs);
        List<CodeBoundMachine> boundMachines = new ArrayList<>();
        List<Integer> machineIds = machines.stream().map(Machine::getMachineId).collect(Collectors.toList());
        if (machineIds.isEmpty()) {
            throw new BaseException(SystemCodeEnum.MACHINE_NOT_EXIST);
        }
        /**已绑定车辆*/
        List<String> boundMachineNOs = new ArrayList<>();
        /**成功车辆*/
        List<String> successMachineNOs = new ArrayList<>();
        /**存在车辆*/
        List<String> nos = machines.stream().map(Machine::getMachineNO).collect(Collectors.toList());
        /**不存在车辆*/
        List<String> failMachineNOs = machineNOs.stream().filter(x -> !nos.contains(x)).collect(Collectors.toList());
        List<Integer> boundIds = codeBoundMachineDao.getByMachineIds(machineIds);
        if (!boundIds.isEmpty()) {
            boundMachineNOs = machines.stream().filter(x -> boundIds.contains(x.getMachineId()))
                    .map(Machine::getMachineNO).collect(Collectors.toList());
            machines = machines.stream().filter(x -> !boundIds.contains(x.getMachineId()))
                    .collect(Collectors.toList());
        }

        Date date = new Date();
        for (Machine machine : machines) {
            if (!purviewService.purviewCheck(loginInfo.id, machine)) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
            CodeBoundMachine codeBoundMachine = new CodeBoundMachine();
            codeBoundMachine.setAdAccountId(machine.getAccountId());
            codeBoundMachine.setMachineId(machine.getMachineId());
            codeBoundMachine.setCodeId(codeId);
            codeBoundMachine.setAddTime(date);
            boundMachines.add(codeBoundMachine);
            successMachineNOs.add(machine.getMachineNO());
        }
        if (!boundMachines.isEmpty()) {
            int i = boundMachines.size() / 300;
            int m = boundMachines.size() % 300;
            for (int k = 0; k < i; k++) {
                codeBoundMachineDao.addBatch(boundMachines.subList(k * 300, (k + 1) * 300));
            }
            if (m > 0) {
                codeBoundMachineDao.addBatch(boundMachines.subList(i * 300, boundMachines.size()));
            }
        }
        return batchOperationMsgVO.setSuccessList(successMachineNOs).setFailList(failMachineNOs).setAbnormalList(boundMachineNOs);
    }

    @Override
    public void del(List<String> machineNOs, LoginInfo loginInfo) {
        List<Machine> machines = machineService.getByMachineNOs(machineNOs);
        for (Machine machine : machines) {
            if (!purviewService.purviewCheck(loginInfo.id, machine)) {
                throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
            }
        }
        List<Integer> machineIds = machines.stream().map(Machine::getMachineId).collect(Collectors.toList());
        if (machineIds.isEmpty()) {
            return;
        }
        codeBoundMachineDao.del(machineIds);
    }

    @Override
    public Integer getByCodeId(Integer codeId) {
        return codeBoundMachineDao.getByCodeId(codeId);
    }


}
